var Tool = require('common/tool');
var Config = require('common/config');
var syllabusPlan = require('common/syllabusPlan');
var navlist = require('./navList');

require('sass/reset.scss');
require('sass/tool.scss');
Tool.avalonForIe7(avalon);
require('../../../directives/count-directive');
require('../../../directives/digits-directive');
require('filters/filter');

require('comp/header');
require('comp/banner');
require('comp/userInfo');
require('comp/nav');
require('comp/footer');
require('comp/footerFloat');
require('./index.scss');
require('./router');

/* 创建avalon数据模型 */
var rootVm = avalon.define({
    $id: 'global',
    navShowArr:"",
    navlist:navlist.list,
    navVisible:false,
    user:{
        name:'',                        // 账户名称
        account:'',                     // 账号ID
        capital:'',                     // 余额
        capitalUnit:'',                 // 币种
        lastLoginTime:''                // 上次登录时间
    },
    account:{
        withdrawOrderNum: 0,              //取款申请个数
        accountUpgradeOrderNum: 0,        //账户级别变更申请个数
        sumProposalNum: 0,                //存款取款账户级别总个数
        depositOrderNum: 0                //存款申请个数
    },
    serverTime:"",                         // 服务器时间
    text:{
        depositText: Config.depositText,
        extractText: Config.extractText
    },
    personal_configurgent:false,                    // 紧急平仓是否开启
    bannerList:[],
    route:{}
});

window['rootVm'] = rootVm;

rootVm.$watch('onReady',function(v){
    new Global();
});

/* 全局方法，包含头部、左侧、底部 */
function Global(){
    this.init();
}

Global.prototype.init = function(){
    this.getIndexInfo();
    this.accountInfo();
    this.adImages();
}
/* 获取用户信息，及其菜单信息 */
Global.prototype.getIndexInfo = function(){
    var that = this;
    Tool.ajax({
        url:'/index',
        type:"post"
    }).done(function(res){
        var code = res.code;
        if(!res || code!='0'){
            layer.alert(res.msg);
            return false;
        }
        var data = res.data,
            custParam  = data.custParam;
        if( data.activityUrl ){
            var acUrl = "https://www.24k.hk/lp_v215_hd_201708GTS2MT4kh.html"+data.activityUrl;
            $("#loginPrev").before('<a href="'+acUrl+'" class="newUserAc" target="_blank"><i class="zj-icon"></i>点击我！领取新人红包！</a>');
        }
        rootVm.serverTime= data.serverTime;
        that.liveBroadcast(rootVm.serverTime);

        rootVm.personal_configurgent = data.personal_configurgent;
        rootVm.user={
            name:custParam.chineseName,
            account:custParam.customerNumber,
            lastLoginTime:custParam.lastLoginTime,
            capital:'',
            capitalUnit:''
        }
        rootVm.navShowArr = data.menuCodeList;
        that.reInitNav(data.menuCodeList, data.personal_configurgent);
    });
}
/* 重新渲染左侧菜单 */
Global.prototype.reInitNav = function(_codeList, personal_configurgent){
    navlist.list.forEach(function(v,i){
        if(v.sub){
            v.sub.forEach(function(sub,subi){
                if(_codeList.indexOf(sub.code)===-1) v.sub.splice(subi,1);
            });
        }
        if(_codeList.indexOf(v.code)===-1){
            navlist.list.splice(i,1);
        }
    });
    rootVm.navlist = navlist.list;
    rootVm.navVisible = true;
    if( personal_configurgent ) rootVm.navlist.push(navlist.closePosition);
}

/* 获取账户金额信息以及对应完成事项数据 */
Global.prototype.accountInfo = function(){
    var that = this;
    Tool.ajax({
        url:'/getAccountInfoAjax',
        type:"post"
    }).done(function(res){
        var code = res.code;
        if(!res || code!='0'){
            layer.alert(res.msg);
            return false;
        }
        var data = res.data,
            accountFundsSumMap  = data.accountFundsSummaryMap;
        rootVm.user.capitalUnit = accountFundsSumMap.currency;     // 币种： 人名币或美元
        rootVm.user.capital = accountFundsSumMap.totalMoney;                    // 余额
        rootVm.account = {
            withdrawOrderNum: data.withdrawOrderNum,              //取款申请个数
            accountUpgradeOrderNum: data.accountUpgradeOrderNum,        //账户级别变更申请个数
            sumProposalNum: data.sumProposalNum,                //存款取款账户级别总个数
            depositOrderNum: data.depositOrderNum                //存款申请个数
        }
    });
}

/* 加载图片广告位 */
Global.prototype.adImages = function(){
    var that = this;
    Tool.ajax({
        url:'/getAdImage',
        type:"post"
    }).done(function(res){
        var code = res.code;
        if(!res || code!='0') return false;
        var data = res.data,
        frameImageList  = data.frameImageList;
        rootVm.bannerList = frameImageList;
    });
}

/* 接口获取直播间消息通知 */
Global.prototype.liveBroadcast = function(serverTime){
    var that = this;
    // var _api = 'http://pmapi.24k.hk/api/common/getCourse?type=pm&platform=uce&groupType=studio&companyId=pm&flag=W';
    var _api = 'http://192.168.35.81:3000/api/common/getCourse?type=pm&platform=uce';
    
    $.get(_api).done(function(res){
        if (res.result === 0) {
            that.getSyllabusPlanTimed(JSON.stringify(res), serverTime);
        }
    });
}
//定时读取直播间信息
var offset = new Date().getTimezoneOffset() * 60000;
Global.prototype.getSyllabusPlanTimed = function (courseData, delta) { 
    var _href = 'http://pmchat.24k.hk?utm_source=n15&utm_medium=ag&utm_campaign=studio&utm_content=gts2_LoginForex_wenzilian';
    var today = new Date(delta);
    var utc = today.getTime() + offset;
    var serverDate=new Date(utc + (3600000 * 8));
    var course=syllabusPlan.getSyllabusPlan(courseData,serverDate);
    //console.log(course);
    var seedroomHTML="";
    var headHTML="";
    if(course==null){
        seedroomHTML = "<i class='zb-icon'></i>本周课程已结束，更多精彩尽在金道直播间！";
        $("#seedroom").html('<a href="'+_href+'" target="_blank">'+seedroomHTML+'</a>');
        return;
    }
    if(course.courseType=='0'){
        headHTML='<i class="zb-icon text-icon"></i>';
    }else{
        headHTML='<i class="zb-icon"></i>';
    }
    if(course.isNext){
        seedroomHTML=headHTML+'当前暂无直播，请关注下节课<span>直播时间：'+ getWeek(course.day)+' '+course.startTime+'-'+course.endTime+'</span><a class="zb-tips">'+ course.lecturer+'：'+course.title +'</a>';
    }else{
        seedroomHTML=headHTML+'正在直播<span>直播时间：'+ getWeek(course.day)+' '+course.startTime+'—'+course.endTime+'</span><a class="zb-tips">'+ course.lecturer+'：'+course.title +'</a>';
    }
    $("#seedroom").html('<a href="'+_href+'" target="_blank">'+seedroomHTML+'</a>');
}

//根据数字获取星期几
function getWeek(day){
    var arr_week=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    return arr_week[day];
}